﻿Public Class BuscMateriales

    Dim classController As New Class_RC_Controller.rc_Materiales()
    Dim proveedores As New Class_RC_Controller.rc_Proveedores

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codMaterial As Integer = 0

    Public Property codMaterial() As Integer
        Get
            Return _codMaterial
        End Get
        Set(value As Integer)
            _codMaterial = value
        End Set
    End Property

    Private _TipoMaterial As String = ""

    Public Property TipoMaterial() As String
        Get
            Return _TipoMaterial
        End Get
        Set(value As String)
            _TipoMaterial = value
        End Set
    End Property

    Private _Costo As Decimal = 0

    Public Property costo() As Decimal
        Get
            Return _Costo
        End Get
        Set(value As Decimal)
            _Costo = value
        End Set
    End Property

    Private _unidMedida As String = ""

    Public Property UnidadMedida() As String
        Get
            Return _unidMedida
        End Get
        Set(value As String)
            _unidMedida = value
        End Set
    End Property

    Private Sub CargarMateriales()

        Dim codTipo As Integer
        If cbxTipo.Checked Then
            codTipo = cboTipoMaterial.SelectedValue
        End If

        Dim codProveedor As Integer
        If cbxProveedor.Checked Then
            codProveedor = cboProveedor.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Materiales_All(oiml.dbConn, 0, txtMaterial.Text, codTipo)
        dgMateriales.DataSource = dt

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarMateriales()
    End Sub

    Private Sub BuscMaterial_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgMateriales.AutoGenerateColumns = False
        CargarMateriales()
    End Sub

    Private Sub txtMaterial_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMaterial.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cbxTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cboTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipoMaterial.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cbxProveedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxProveedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cboProveedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboProveedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub dgMateriales_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgMateriales.KeyDown
        If e.KeyCode = Keys.Enter Then
            Seleccionar()
        End If
    End Sub

    Private Sub Seleccionar()
        Try
            If dgMateriales.SelectedRows.Count > 0 Then

                codMaterial = dgMateriales.SelectedRows(0).Cells(0).Value
                TipoMaterial = dgMateriales.SelectedRows(0).Cells(1).Value
                UnidadMedida = dgMateriales.SelectedRows(0).Cells(6).Value
                costo = dgMateriales.SelectedRows(0).Cells(7).Value

                Me.Close()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private Sub dgMateriales_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgMateriales.CellDoubleClick
        Seleccionar()
    End Sub

End Class